package com.aptemo.webetek.client.widgets.games;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.enums.EnumEventStatus;
import com.aptemo.webetek.client.utils.TokenUtils;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingView;
import com.aptemo.webetek.shared.EventBean;
import com.aptemo.webetek.shared.LabelBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class Games
		extends Composite implements GamesDisplay
{
	private static GamesUiBinder	uiBinder	= GWT.create(GamesUiBinder.class);

	interface GamesUiBinder extends UiBinder<Widget, Games>
	{
	}

	// @UiField
	// HTMLPanel commandPanel;

	@UiField
	FlexTable	gameList;

	@UiField
	Button		createNewGameButton;

	@UiField
	ListBox		statusFilter;

	@UiField
	ListBox		labelsFilter;

	@UiField
	PagingView	pagingPanel;

	@UiField
	Label		infoField;

	public Games()
	{
		initWidget(uiBinder.createAndBindUi(this));

		// status list box
		// add all option
		// statusFilter.addItem(Webetek.getConstants().all(), "");
		for (EnumEventStatus item : EnumEventStatus.values())
		{
			statusFilter.addItem(translate(item.toString()), item.toString());
		}

		labelsFilter.addItem(Webetek.getConstants().all(), "");
	}

	@Override
	public void addAdminButtons()
	{
		createNewGameButton.setVisible(true);
	}

	@Override
	public HasClickHandlers getCreateGameBtn()
	{
		return createNewGameButton;
	}

	@Override
	public void updateBets(List<EventBean> list, String teamId, HandlerManager eventbus, WebetekUserServiceAsync rpc)
	{
		gameList.removeAllRows();

		if (list.size() == 0)
		{
			pagingPanel.setVisible(false);
			gameList.setText(0, 0, Webetek.getConstants().noEntries());
			return;
		}
		// selBar.setVisible(true);
		pagingPanel.setVisible(true);

		int i = 0;
		for (EventBean item : list)
		{

			GameRow row = new GameRow(item);
			GameRowPresenter presenter = new GameRowPresenter(row, item, rpc, eventbus);
			presenter.go(null);
			gameList.setWidget(i++, 0, row);
		}

	}

	@Override
	public HasChangeHandlers getStatusList()
	{
		return statusFilter;
	}

	@Override
	public EnumEventStatus getStatusFieldValue()
	{
		int i = statusFilter.getSelectedIndex();

		String s = statusFilter.getValue(i);
		if (s.isEmpty())
		{
			return null;
		}

		return EnumEventStatus.valueOf(s);
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasClickHandlers getList()
	{
		return gameList;
	}

	@Override
	public int getClickedRow(ClickEvent event)
	{
		int selectedRow = -1;
		HTMLTable.Cell cell = gameList.getCellForEvent(event);

		if (cell != null)
		{
			// Suppress clicks if the user is actually selecting the
			// check box
			//
			if (cell.getCellIndex() >= 0)
			{
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public void setLabels(List<LabelBean> data, String id)
	{
		labelsFilter.clear();
		labelsFilter.addItem(Webetek.getConstants().all(), "");
		int i = 1;
		int selind = 0;
		for (LabelBean item : data)
		{
			labelsFilter.addItem(item.getTitle(), item.getId());
			if (item.getId().equals(id))
			{
				selind = i;
			}
			i++;
		}
		labelsFilter.setSelectedIndex(selind);
		// ChangeEvent.fireNativeEvent(Document.get().createChangeEvent(),
		// labelsFilter);
	}

	@Override
	public HasChangeHandlers getLabels()
	{
		return labelsFilter;
	}

	@Override
	public String getSelectedLabelId()
	{
		int i = labelsFilter.getSelectedIndex();
		if (i < 0)
			return "";

		return labelsFilter.getValue(i);
	}

	@Override
	public PagingDisplay getPagingView()
	{
		return pagingPanel;
	}

	@Override
	public void setStatusValue(EnumEventStatus status)
	{
		for (int i = 0; i < statusFilter.getItemCount(); i++)
		{
			String val = statusFilter.getValue(i);

			if (!val.isEmpty() && EnumEventStatus.valueOf(val) != null && EnumEventStatus.valueOf(val).equals(status))
			{
				statusFilter.setSelectedIndex(i);

				String token = History.getToken();
				if (token.contains("group"))
				{
					token = TokenUtils.setToken(token, "filter", val);
					History.newItem(token, false);
				}
				return;
			}
		}

	}

	@UiHandler("statusFilter")
	void onStatusChanged(ChangeEvent event)
	{
		String token = History.getToken();

		if (getStatusFieldValue() != null)
		{
			token = TokenUtils.setToken(token, "filter", getStatusFieldValue().toString());
		}
		else
		{
			token = TokenUtils.setToken(token, "filter", "");
			// token = TokenUtils.removeToken(token, "filter");
		}
		// reset paging...
		token = TokenUtils.removeToken(token, "page");
		History.newItem(token);
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}


}
